﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dal
{
    public class MemberDal
    {
        public MemberDal(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }

        public int AddMember(Member member)
        {
            try
            {
                Context.Members.Add(member);
                return Context.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }

        public  int GetMemberPhone(string Phone)
        {
            try
            {
                int cha = Context.Members.Where(x => x.Phone == Phone).Count();
                return cha;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public List<Member> GetMembers(string? phone = "",string?name="",DateTime?date1=null,DateTime?date2=null)
        {
            List<Member> list=Context.Members.Where(x=>x.Delete==false).ToList();
            if(!string.IsNullOrEmpty(phone))
            {
                list=list.Where(x=>x.Phone==phone).ToList();

            }

            if(!string.IsNullOrEmpty(name))
            {
                list=list.Where(x=>x.Name.Contains(name)).ToList();
            }

            if(date1!=null&&date2!=null)
            {
                list=list.Where(x=>x.Sdate>=date1&&x.Edate<=date2.Value.AddDays(1)).ToList();
            }
            return list;
        }
    }
}
